<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Models\permission;
use Illuminate\Support\Facades\Cache;
use App\Modules\Admin\Http\Requests\PermissionCreateRequest;
use App\Modules\Admin\Http\Requests\PermissionUpdateRequest;
use App\Modules\Admin\Http\Controllers\BaseController;
use App\Services\Admin\PermissionService;
use Illuminate\Http\Request;


class PermissionController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $permissionId = $request->get('permissionId');
        $param = $this->getParam('Permission','Permission/getList');
        return view('admin::permission.index', compact('permissionId','param'));
    }

    /**
     * 获取列表页
     * @param Request $request
     * @method getList
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getList(Request $request){
        Cache::forever('Permission/getList', $request->server->get('QUERY_STRING'));
        $parent_id = $request->input('parent_id',0);
        $page = $request->input('page', 1);
        $per_page = $request->input('limit', 10);
        $permissionService = new PermissionService();
        $result = $permissionService->getList($parent_id,$page,$per_page);
        if($result['status']==1){
            return layTableApi($result['data']['total'],$result['data']['data']);
        }else{
            return layTableApi(0,[],1);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $permissionId = $request->get('id', 0);
        $permissions = $this->permissionTree();
        return view('admin::permission.create',  compact('permissions', 'permissionId'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param PermissionCreateRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(PermissionCreateRequest $request)
    {
        $data = $request->all();
        $permissionService = new PermissionService();
        $result = $permissionService->createPermission($data);
        Cache::forever('Permission', true);
        return apiResponse($result['msg'],$result['status'],$result['data']);
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $permission = permission::findOrFail($id);
        $permissions = $this->permissionTree();
        return view('admin::permission.edit', compact('permission', 'permissions'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param PermissionUpdateRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */

    public function update(PermissionUpdateRequest $request, $id)
    {
        $data = $request->all();
        $permissionService = new PermissionService();
        $result = $permissionService->updatePermission($id,$data);
        Cache::forever('Permission', true);
        return apiResponse($result['msg'],$result['status'],$result['data']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return apiResponse('请选择删除项');
        }
        $permission = Permission::find($ids[0]);
        if (!$permission) {
            return apiResponse('权限不存在');
        }
        //如果有子权限， 则禁止删除
        if (Permission::where('parent_id', $ids[0])->first()) {
            return apiResponse('存在子权限禁止删除');
        }

        if ($permission->delete()) {
            return apiResponse('删除成功',1,[]);
        }
        return apiResponse('删除失败');
    }
}
